package com.hxy;

import com.hxy.publish.Publisher;
import com.hxy.subscribe.SubThread;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Main {
    public static void main(String[] args) throws IOException {
        // 连接redis服务端
        JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "127.0.0.1", 6379);
        System.out.println(String.format("Redis连接池正在启动, Redis IP：%s, Redis端口：%d", "127.0.0.1", 6379));

        //订阅者
        int num = 3;
        ExecutorService executor = Executors.newFixedThreadPool(num);
        for (int i = 0; i < num; i++) {
            int finalI = i;
            Future<Integer> future = executor.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    SubThread subThread = new SubThread(jedisPool);
                    subThread.newsSubscription("订阅者" + finalI);
                    return 3;
                }
            });
        }

        //发布者
        Publisher publisher = new Publisher(jedisPool);
        publisher.newsPublish();
    }
}
